import router from './router'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css' // 可以进去修改样式
import store from '@/store/index'

// 白名单
const whiteList = ['/login', '/register', '/404']
router.beforeEach((to, from, next) => {
  nprogress.start()
  // 有token
  if (store.state.user.token) {
    if (whiteList.includes(to.path)) {
      next('/')
      nprogress.done()
    } else {
      store.dispatch('user/getProfileAction')

      next()
    }
    // 无token
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      nprogress.done()
    }
  }
})
router.afterEach(() => {
  nprogress.done()
})
